package ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetJpanel extends JPanel implements ActionListener {
    private Icon buttonGet = Util.getimagesIcon("/images/buttonGet.jpg");
    private Icon buttonOff = Util.getimagesIcon("/images/buttonOff.jpg");
    private JLabel jLabelGet;
    private JButton jButtonGet;
    private JTextField jTextFieldGet;
    private JButton btnOff;
    private ATMJframe atmJframe;

    public GetJpanel(ATMJframe atmJframe) {
        this.atmJframe = atmJframe;
        this.setLayout(null);
        this.setOpaque(false);
        jLabelGet = new JLabel("请输入取款金额：");
        jLabelGet.setBounds(300, 200, 250, 50);
        jLabelGet.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jLabelGet);

        jTextFieldGet = new JTextField();
        jTextFieldGet.setBounds(520, 200, 300, 50);
        jTextFieldGet.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jTextFieldGet);

        jButtonGet = new JButton("", buttonGet);
        jButtonGet.setBounds(300, 250, 140, 50);
        this.add(jButtonGet);
        jButtonGet.addActionListener(this);


        btnOff = new JButton("", buttonOff);
        btnOff.setBounds(850, 550, 140, 50);
        this.add(btnOff);
        btnOff.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        double a = Double.parseDouble(jTextFieldGet.getText());

        if (e.getSource() == btnOff) {
            atmJframe.switchPanel("menu");
        } else if (e.getSource() == jButtonGet) {
            if (a > atmJframe.getUser().getCount()) {
                JOptionPane.showMessageDialog(this, "余额不足");
            } else {
                JOptionPane.showMessageDialog(this, "取款成功，取出"+a+"元");

                double moneyOrigin = atmJframe.getUser().getCount();
                String id = atmJframe.getUser().getId();
                //修改数据库
                atmJframe.getTest01().sub(moneyOrigin,a,id);
                //修改User
                atmJframe.getUser().setCount(moneyOrigin - a);


//                atmJframe.getUser().setCount(atmJframe.getUser().getCount()-Double.parseDouble(jTextFieldGet.getText()));

//                FileUtil.writeObject("a.dat",atmJframe.getUsersArrayList());

            }
        }
    }
}








